<?php
App::import('Sanitize');
class MessagesController extends AppController {

	var $helpers = array('Message');
	var $check_int = "/[0-9]/";

	function inbox() {
		$messages = $this->Message->get_inbox($this->Session->read('Auth.User.username'));
		//$messages = Sanitize::clean($messages, array('escape' => true, 'carriage' => true, 'encode' => false));
		$this->set(compact('messages'));
	}

	function view($id = null) {
		if ($conversation = $this->Message->get_conversation($id)) {
			$conversation = Sanitize::clean($conversation, array('encode' => false));
			$this->set('conversation', $conversation);
		} else {
			$this->redirect(array('action' => 'inbox'));
		}
	}
	
	function send($to_user = null) {
		if (!empty($this->data)) {
			if(strtolower($this->data['Message']['to']) != strtolower($this->Session->read('Auth.User.username'))){
				if ($this->Message->send($this->data)) {
					$this->Session->setFlash('Uw bericht is succesvol verzonden!');
					$this->redirect(array('action' => 'inbox'));
				} else {
					$this->Session->setFlash('Niet alle velden zijn ingevuld.');
				}
			} else { 
				$this->Session->setFlash('U kunt geen bericht naar uw zelf sturen.');
			}
		} else {
			if ($to_user !=null) {
				if (preg_match($this->check_int, $to_user)){
					if ($message = $this->Message->get_reply_data($to_user)) {
						$this->data = $message;
					} else {
						$this->Session->setFlash('Don\'t try to hack into someone else\'s conversation!');
						$this->Redirect(array('controller' => 'messages', 'action' => 'inbox'));
					}
				} else { 
					$this->data['Message']['to'] = $to_user;
					$this->data['Message']['subject'] = null;
				}
			}
		}
	}
	
	function send_item($type = null, $id = null){
	if (!empty($this->data)) {
		if ($this->Message->send($this->data)) {
			$this->Session->setFlash(__('Bericht is verzonden!', true));
			$this->redirect(array('controller' => 'pitches', 'action' => 'index'));
		}
	} else{
		if ($id != null){
			if($type == 'opdracht'){
				$this->loadModel('Pitch');
				$pitch = $this->Pitch->read(null, $id);
				$this->data['Message']['subject'] = $pitch['Pitch']['title'];
				$this->data['Message']['body'] = $pitch['Pitch']['description'];
				$this->data['Message']['id'] = $pitch['Pitch']['id'];
			} else { 
				$this->loadModel('Job');
				$job = $this->Job->read(null, $id);
				$this->data['Message']['subject'] = $job['Job']['title'];
				$this->data['Message']['body'] = $job['Job']['description'];
				$this->data['Message']['id'] = $job['Job']['id'];
			}
		} else {
			$this->redirect(array('controller' => 'pitches', 'action' => $id));
		}
		}
	}
	
	function delete($id = null) {
		if ($this->Message->delete_conversation($id)) {
			$this->Session->setFlash(__('De conversatie is verwijderd', true));
			$this->redirect(array('controller' => 'messages', 'action' => 'inbox'));
		} else {
			$this->Session->setFlash(__('Er is iets mis gegaan, probeer het nog een keer', true));
		}
	}
	
	function not_read() {
		$not_read_count = $this->Message->get_not_read();
		if (isset($this->params['requested'])) {
			return $not_read_count;
		}
	}
			

}
?>
